package com.numberone.web.controller.cart;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.numberone.common.annotation.Log;
import com.numberone.common.enums.BusinessType;
import com.numberone.system.domain.Cart;
import com.numberone.system.service.ICartService;
import com.numberone.framework.web.base.BaseController;
import com.numberone.common.page.TableDataInfo;
import com.numberone.common.base.AjaxResult;
import com.numberone.common.utils.poi.ExcelUtil;

/**
 * 购物车 信息操作处理
 * 
 * @author numberone
 * @date 2020-09-29
 */
@Controller
@RequestMapping("/system/cart")
public class CartController extends BaseController
{
    private String prefix = "system/cart";
	
	@Autowired
	private ICartService cartService;
	
	@RequiresPermissions("system:cart:view")
	@GetMapping()
	public String cart()
	{
	    return prefix + "/cart";
	}
	
	/**
	 * 查询购物车列表
	 */
	@RequiresPermissions("system:cart:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Cart cart)
	{
		startPage();
        List<Cart> list = cartService.selectCartList(cart);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出购物车列表
	 */
	@RequiresPermissions("system:cart:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Cart cart)
    {
    	List<Cart> list = cartService.selectCartList(cart);
        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
        return util.exportExcel(list, "cart");
    }
	
	/**
	 * 新增购物车
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存购物车
	 */
	@RequiresPermissions("system:cart:add")
	@Log(title = "购物车", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Cart cart)
	{		
		return toAjax(cartService.insertCart(cart));
	}

	/**
	 * 修改购物车
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		Cart cart = cartService.selectCartById(id);
		mmap.put("cart", cart);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存购物车
	 */
	@RequiresPermissions("system:cart:edit")
	@Log(title = "购物车", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Cart cart)
	{		
		return toAjax(cartService.updateCart(cart));
	}
	
	/**
	 * 删除购物车
	 */
	@RequiresPermissions("system:cart:remove")
	@Log(title = "购物车", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(cartService.deleteCartByIds(ids));
	}
	
}
